package com.hzj.controller;

import com.hzj.pojo.User;
import com.hzj.service.UserService;
import com.mysql.jdbc.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.text.ParseException;

@Controller
@RequestMapping("/user")
public class UserController {
    @Autowired
    @Qualifier("userServiceImpl")
    private UserService userService;

    @RequestMapping("/toLogin")
    public String toLogin(){
        return "jsp/login";
    }

    @RequestMapping("/login")
    public String login(HttpServletRequest request,HttpSession session, String userName, String pwd){
        User user = userService.login(userName, pwd);
        if (user!=null){
            session.setAttribute("userInfo",user);
            return "jsp/main";
        }
        else {
            request.setAttribute("msg","用户名或密码错误");
            return "jsp/login";
        }
    }

    @RequestMapping("/logout")
    public String logout(HttpSession session){
        session.removeAttribute("userInfo");
        return "jsp/login";
    }

    @RequestMapping("/toRegister")
    public String toRegister(){
        return "jsp/register";
    }

    @RequestMapping("/register")
    public String register(User user) throws ParseException {
        user.setId();
        user.setCreationDate();
        System.err.println("-------UserController->register->"+user);
        int i = userService.addUser(user);
        if(i>0) {
            return "jsp/login";
        }else return "";
    }
}
